package com.example.sqllite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Sqllite extends Fragment {

    public Sqllite(){
        super(R.layout.sqllite);
    }

    public void onViewCreated(@NonNull View view, @Nullable Bundle saveInstanceState) {
        super.onViewCreated(view, saveInstanceState);

        EditText Name = getActivity().findViewById(R.id.name);
        EditText Age = getActivity().findViewById(R.id.age);
        TextView output = getActivity().findViewById(R.id.output);
        Button insert = (Button)getActivity().findViewById(R.id.Insert);
        Button query = (Button)getActivity().findViewById(R.id.Query);
        Button delete = (Button)getActivity().findViewById(R.id.Delete);

        DbHepler dbHepler = new DbHepler(getContext());
        SQLiteDatabase db = dbHepler.getReadableDatabase();
        StringBuilder stringBuilder = new StringBuilder();
        Cursor cursor = db.query("users", null, null, null, null, null, null);
        if (cursor.getCount() == 0) {
            output.setText("没有数据");
            Toast.makeText(getActivity(), "数据库为空!", Toast.LENGTH_SHORT).show();
        } else {
            while (cursor.moveToNext()) {
                long id = cursor.getLong(cursor.getColumnIndex("id"));
                String username = cursor.getString(cursor.getColumnIndex("username"));
                int age = cursor.getInt(cursor.getColumnIndex("age"));
                stringBuilder.append("序号:").append(id)
                        .append("  姓名:").append(username)
                        .append("  年龄:").append(age)
                        .append("\n");
                output.setText(stringBuilder.toString());
            }
            db.close();
        }

        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHepler.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("username", Name.getText().toString());
                values.put("age", Integer.parseInt(Age.getText().toString()));
                long ret = db.insert("users", null, values);
                String msg = ret == -1 ? "添加失败!" : "添加成功!";
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                db = dbHepler.getReadableDatabase();
                StringBuilder stringBuilder = new StringBuilder();
                Cursor cursor = db.query("users", null, null, null, null, null, null);
                if (cursor.getCount() == 0) {
                    output.setText("没有数据");
                    Toast.makeText(getActivity(), "数据库为空!", Toast.LENGTH_SHORT).show();
                } else {
                    while (cursor.moveToNext()) {
                        long id = cursor.getLong(cursor.getColumnIndex("id"));
                        String username = cursor.getString(cursor.getColumnIndex("username"));
                        int age = cursor.getInt(cursor.getColumnIndex("age"));
                        stringBuilder.append("序号:").append(id)
                                .append("  姓名:").append(username)
                                .append("  年龄:").append(age)
                                .append("\n");
                        output.setText(stringBuilder.toString());
                    }
                    db.close();
                }
            }
        });

        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHepler.getReadableDatabase();
                StringBuilder stringBuilder = new StringBuilder();
                Cursor cursor = db.query("users", null, "age < 18", null, null, null, null);
                if (cursor.getCount() == 0) {
                    output.setText("没有数据");
                    Toast.makeText(getActivity(), "数据库为空!", Toast.LENGTH_SHORT).show();
                } else {
                    while (cursor.moveToNext()) {
                        long id = cursor.getLong(cursor.getColumnIndex("id"));
                        String username = cursor.getString(cursor.getColumnIndex("username"));
                        int age = cursor.getInt(cursor.getColumnIndex("age"));
                        stringBuilder.append("序号:").append(id)
                                .append("  姓名:").append(username)
                                .append("  年龄:").append(age)
                                .append("\n");
                        output.setText(stringBuilder.toString());
                    }
                    db.close();
                }
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHepler.getWritableDatabase();
                String deleteName = Name.getText().toString();
                Cursor cursor = db.query("users", null, null, null, null, null, null);
                int num = cursor.getCount();
                db.delete("users", "username=?", new String[]{deleteName});
                Toast.makeText(getActivity(), "已删除\""+deleteName + "\"的信息", Toast.LENGTH_SHORT).show();
                StringBuilder stringBuilder = new StringBuilder();
                cursor = db.query("users", null, null, null, null, null, null);
                if (cursor.getCount() == 0) {
                    output.setText("没有数据");
                    Toast.makeText(getActivity(), "数据库为空!", Toast.LENGTH_SHORT).show();
                } else {
                    while (cursor.moveToNext()) {
                        long id = cursor.getLong(cursor.getColumnIndex("id"));
                        String username = cursor.getString(cursor.getColumnIndex("username"));
                        int age = cursor.getInt(cursor.getColumnIndex("age"));
                        stringBuilder.append("序号:").append(id)
                                .append("  姓名:").append(username)
                                .append("  年龄:").append(age)
                                .append("\n");
                        output.setText(stringBuilder.toString());
                    }
                    db.close();
                }
            }
        });
    }

    public void AllQuery(){

    }
}
